<?php
/*
  $Id: ht_theme_switcher.php v1.2.1 20120314 Kymation $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2012 osCommerce

  Released under the GNU General Public License
*/

  class ht_theme_switcher {
    var $code = 'ht_theme_switcher';
    var $group = 'header_tags';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;

    function ht_theme_switcher() {
    	global $PHP_SELF;

      $this->title = MODULE_HEADER_TAGS_THEME_SWITCHER_TITLE;
      $this->description = MODULE_HEADER_TAGS_THEME_SWITCHER_DESCRIPTION;

      if ( defined('MODULE_HEADER_TAGS_THEME_SWITCHER_STATUS') ) {
        $this->sort_order = MODULE_HEADER_TAGS_THEME_SWITCHER_SORT_ORDER;
        $this->enabled = (MODULE_HEADER_TAGS_THEME_SWITCHER_STATUS == 'True');
      }

      // Include the function that is used to add products in the Admin
      if( $PHP_SELF == 'modules.php' ) {
        include_once( DIR_WS_FUNCTIONS . 'modules/header_tags/theme_switcher.php');
      }
    }

    function execute() {
      global $oscTemplate;

      $jquery_version = '1.5.1';
      if( MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_VERSION != '' ) {
        $jquery_version = MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_VERSION;
      }

      $jquery_ui_version = '1.8.6';
      if( MODULE_HEADER_TAGS_THEME_SWITCHER_THEME != '' ) {
        $jquery_ui_version = MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_UI_VERSION;
      }

      $theme_name = 'redmond';
      if( MODULE_HEADER_TAGS_THEME_SWITCHER_THEME != '' ) {
        $theme_name = MODULE_HEADER_TAGS_THEME_SWITCHER_THEME;
      }

      $theme_text = '<script type="text/javascript" src="ext/jquery/jquery-' . $jquery_version . '.min.js"></script>' . PHP_EOL;
      $theme_text .= '<script type="text/javascript" src="ext/jquery/ui/jquery-ui-' . $jquery_ui_version . '.min.js"></script>' . PHP_EOL;
      $theme_text .= '<link rel="stylesheet" type="text/css" href="ext/jquery/ui/' . $theme_name . '/jquery-ui-' . $jquery_ui_version . '.css" />' . PHP_EOL;

      $oscTemplate->addBlock( $theme_text, $this->group );
    }

    function isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined( 'MODULE_HEADER_TAGS_THEME_SWITCHER_STATUS' );
    }

    function install() {
    	$this->_load_header_tags_first();
    	
      tep_db_query( "insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Theme Switcher', 'MODULE_HEADER_TAGS_THEME_SWITCHER_STATUS', 'True', 'Do you want to be able to select a theme here?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())" );
      tep_db_query( "insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_HEADER_TAGS_THEME_SWITCHER_SORT_ORDER', '1', 'Sort order of display. Lowest is displayed first.', '6', '2', now())" );
      tep_db_query( "insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Jquery Version', 'MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_VERSION', '1.5.1', 'The version number of your Jquery module (e.g. 1.5.1).', '6', '3', now())" );
      tep_db_query( "insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Jquery UI Version', 'MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_UI_VERSION', '1.8.6', 'The version number of your Jquery UI module (e.g. 1.8.13).', '6', '3', now())" );
      tep_db_query( "insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Theme', 'MODULE_HEADER_TAGS_THEME_SWITCHER_THEME', 'redmond', 'Select the theme that you want to use.', '6', '3', 'tep_cfg_pull_down_themes(', now())" );
    }

    function remove() {
      tep_db_query( "delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
    	$keys = array();

      $keys[] = 'MODULE_HEADER_TAGS_THEME_SWITCHER_STATUS';
      $keys[] = 'MODULE_HEADER_TAGS_THEME_SWITCHER_SORT_ORDER';
      $keys[] = 'MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_VERSION';
      $keys[] = 'MODULE_HEADER_TAGS_THEME_SWITCHER_JQUERY_UI_VERSION';
      $keys[] = 'MODULE_HEADER_TAGS_THEME_SWITCHER_THEME';

      return $keys;
    }
    
    // Force the header tags to load first, so the jQuery and jQuery UI code is available 
    //   to other scripts that load in the head section
    function _load_header_tags_first() {
    	// If header_tags is not the first item on the list
    	if( substr( TEMPLATE_BLOCK_GROUPS, 0, 11 ) != 'header_tags' ) {
    		// Remove header_tags from wherever it is in the list
    		$template_block_groups = str_replace( ';header_tags', '', TEMPLATE_BLOCK_GROUPS );
    		// And add header_tags back onto the front of the list
        $template_block_groups = 'header_tags;' . $template_block_groups;
        $sql_data_array = array( 'configuration_value' => $template_block_groups );
        // Update the database with the fixed string
        tep_db_perform( TABLE_CONFIGURATION, $sql_data_array, 'update', "configuration_key = 'TEMPLATE_BLOCK_GROUPS'" );
    	} // if( substr
    } // function _load_header_tags_first
    
  } // end class
  
?>